//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ConfigurationWizard.Configuration;
using System.Configuration;
using ConfigurationWizard.Setup;
using System.Collections.ObjectModel;

namespace ConfigurationWizard
{
    public partial class Information : UserControl
    {
        public Information()
        {
            InitializeComponent();
        }

        private void titleLabel_Click(object sender, EventArgs e)
        {

        }

        private void Welcome_Load(object sender, EventArgs e)
        {
            LoadControls();
            ((Shell)this.Parent.Parent).nextButton.Focus();
        }

        private void LoadControls()
        {
            SetupSection section = (SetupSection)ConfigurationManager.GetSection("setup");
            SetupConfigurationReader reader = new SetupConfigurationReader(section);
            Collection<Preview> previews = reader.GetPreviewForThisOS();

            int height = 0;
            int marginlabel = 2;
            int margincheck = 2;
            int lines = 1;
            foreach (Preview p in previews)            
            {
                               
                CheckBox checkBox = new CheckBox();                                
                checkBox.AutoSize = true;
                checkBox.Checked = true;
                checkBox.CheckState = System.Windows.Forms.CheckState.Checked;
                checkBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                checkBox.Size = new System.Drawing.Size(199, 17);                                
                checkBox.Location = new System.Drawing.Point(15, height);
                height += checkBox.Height + marginlabel;              
                checkBox.Text = p.Name;
                checkBox.UseVisualStyleBackColor = true;

                Label label = new Label();
                label.AutoSize = true;                
                label.Text = p.Description;
                lines = CountOcurrence(p.Description, Environment.NewLine) + 1;
                label.Size = new System.Drawing.Size(258, 16 * lines);
                label.Location = new System.Drawing.Point(34, height);
                height += label.Height + margincheck;
                                
                this.panel1.Controls.Add(checkBox);
                this.panel1.Controls.Add(label);
            }                   
        }

        private static int CountOcurrence(string inString, string secuence)
        {            
            int count = 0;
            int max = inString.Length - secuence.Length + 1;
            for (int i = 0; i < max; i++)
            {
                if (String.Compare(inString.Substring(i, secuence.Length), secuence, StringComparison.InvariantCulture) == 0)
                    count++;
            }
            return count;
        }
    }
}
